package Net
{
	import ClientProtocal.ClientProtocalInterfacePrx;
	import ClientProtocal.UserLoginPrx;
	import ClientProtocal.messageCBPrx;
	import ClientProtocal.messageCBPrxHelper;
	
	import Ice.*;
	
	import Net.RecvMessageCBCL;
	
	
	//网络控制管理 
	public class NetManagerCL
	{
		private var m_initData           :Ice.InitializationData                    ;
		private var m_ice                :Ice.Communicator                          ;
		private var m_adapter            :Ice.ObjectAdapter                         ;
		
		private var m_userLoginPrx       :ClientProtocal.UserLoginPrx               ;
		private var m_protocalCreaterPrx :ClientProtocal.ClientProtocalInterfacePrx ;
		
		public function NetManagerCL():void
		{
		}
		
		public function Initial():void
		{
			m_initData = new Ice.InitializationData() ;
			m_initData.properties = Ice.Util.createProperties();
			m_initData.properties.setProperty("Ice.Warn.Connections", "1");
			m_initData.properties.setProperty("Callback.Client.Endpoints", "tcp");
			
			try
			{
				var host:String ;
				host = "127.0.0.1" ;
				m_initData.properties.setProperty("Ice.Default.Host" , host) ;
				
				//初始化 ICE
				m_ice = Ice.Util.initialize(m_initData) ;
				
				//创建 到服务器的连接  登录链接 
				var obj:Ice.ObjectPrx = m_ice.stringToProxy("ClientProtocalLogin:tcp -h 192.168.1.98 -p 10001") ;
				ClientProtocal.UserLoginPrxHelper.checkedCast(obj.ice_twoway()).whenCompleted(UserLoginCreate , UserLoginException) ;
			}
			catch(e:Ice.Exception)
			{
			}
		}
		//成功创建登录连接
		private function UserLoginException(r_:Ice.AsyncResult, e_:Ice.Exception):void
		{
			//_logger.print(ex.getStackTrace());
		}
		private function UserLoginCreate(r_:Ice.AsyncResult , prx_:ClientProtocal.UserLoginPrx):void
		{
			m_userLoginPrx = prx_ ;
			
			//创建 适配器 
			m_ice.createObjectAdapter("Callback.Client").whenCompleted(AdapterCreate , AdapterCreateException) ;
		}
		
		private function AdapterCreateException(r_:Ice.AsyncResult, e_:Ice.Exception):void
		{
			//_logger.print(ex.getStackTrace());
		}
		private function AdapterCreate(r_:Ice.AsyncResult , obj_:Ice.ObjectAdapter):void
		{
			m_adapter = obj_ ;
			
			//创建服务器回调接口 
			var cb:RecvMessageCBCL = new  RecvMessageCBCL() ;
			var messageCBPrx:ClientProtocal.messageCBPrx = ClientProtocal.messageCBPrxHelper.uncheckedCast(m_adapter.add(cb , m_ice.stringToIdentity("messageCB"))) ;
			
			//登录服务器 
			m_userLoginPrx.Login("jamesnwes" , "123456" , messageCBPrx).whenCompleted(RecvClientInterfaceCreater , RecvClientInterfaceCreaterException) ;
		}
		private function RecvClientInterfaceCreaterException(r_:Ice.AsyncResult, e_:Ice.Exception):void
		{
			//_logger.print(ex.getStackTrace());
		}
		private function RecvClientInterfaceCreater(r_:Ice.AsyncResult , prx_:ClientProtocal.ClientProtocalInterfacePrx):void
		{
			m_protocalCreaterPrx = prx_ ;
			
            //创建客户端接收协议
		}
	}
}